package com.hy.service.impl;

import com.hy.bean.Logintable;
import com.hy.bean.RespBean;
import com.hy.dao.LogintableDao;
import com.hy.dao.impl.LogintableDaoImpl;
import com.hy.service.LogintableService;
import com.hy.utils.JwtUtils;
import com.hy.vo.LogintableEmpVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class LogintableServiceImpl implements LogintableService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final LogintableDao logintableDao = new LogintableDaoImpl();

    @Resource
    private JwtUtils jwtUtils;

    /* @Description: 根据用户名查询
    * @Date: 2024/5/14 23:37
    * @paramType: String
    * @Param: name 用户名
    * @Return: Logintable
    */
    @Override
    public RespBean queryByLoginName(Logintable logintable) {
        logger.debug("queryById===logintable===>{}",logintable);
        LogintableEmpVo logintableEmpVo = logintableDao.queryByLoginname(logintable.getLoginName());
        // 登录成功
        if (logintableEmpVo != null) {
            if (logintableEmpVo.getPassword().equals(logintable.getPassword()) ||
                    logintableEmpVo.getPassword() == logintable.getPassword()) {
                String token = jwtUtils.createJwt(logintableEmpVo.getLoginName());
                logintableEmpVo.setPassword("");
                Map<String, Object> map = new HashMap<>();
                map.put("token", token);
                map.put("loginData", logintableEmpVo);
                return RespBean.success("登录成功", map);
            }
        }else {
            return RespBean.error("该用户不存在");
        }
        // 登录失败
        return RespBean.error("账号密码错误");
    }

    @Override
    public RespBean queryByLoginByEmpId(Integer loginName) {
        Logintable logintable = logintableDao.queryByEmpId(loginName);
        if (logintable != null){
            return RespBean.success(null,logintable);
        }

        return RespBean.success("该用户不存在",500);
    }

    @Override
    public RespBean save(Logintable logintable){
        if (logintableDao.insert(logintable) > 0){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    public RespBean querLimitsOfAuthorityByName(String loginName) {
        return null;
    }

}